package juc.toolclass;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class MyCountDownLatch {
    public static class T extends Thread{
        int sleepSecond;
        CountDownLatch countDownLatch;
        public T(String name,int sleepSecond, CountDownLatch countDownLatch){
            super(name);
            this.sleepSecond=sleepSecond;
            this.countDownLatch=countDownLatch;
        }

        @Override
        public void run() {
            Thread thread = Thread.currentThread();
            long startTime = System.currentTimeMillis();
            try {
                TimeUnit.SECONDS.sleep(sleepSecond);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                countDownLatch.countDown();
            }
            long endTime = System.currentTimeMillis();
            System.out.println(thread.getName()+"处理完毕,耗时："+(endTime-startTime));
        }
    }

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(2);

        long startTime = System.currentTimeMillis();
        T t1 = new T("t1", 2, countDownLatch);
        t1.start();

        new T("t2",5,countDownLatch).start();

        countDownLatch.await();

        long endTime = System.currentTimeMillis();
        System.out.println("总耗时："+(endTime-startTime));

    }
}
